<template>
  <div class="blog-category-container">
    <h2>文章分类</h2>
    <right-list :list="list" @selected="handleSelect"></right-list>
  </div>
</template>

<script>
import RightList from "./RightList.vue";
import fetchData from "@/mixins/fetchdata.js";
import { getBlogTypes } from "@/api/blog.js";
export default {
  name: "BlogCategory",
  mixins: [fetchData([])],
  components: {
    RightList,
  },
  computed: {
    categoryId() {
      return parseInt(this.$route.params.categoryId) || -1;
    },
    limit() {
      return +this.$route.query.limit || 10;
    },
    list() {
      const totalArticleCount = this.data.reduce(
        (a, b) => a + b.articleCount,
        0
      );
      const result = [
        { name: "全部", id: -1, articleCount: totalArticleCount },
        ...this.data,
      ];
      let data = result.map((item) => {
        return {
          ...item,
          isSelect: item.id == this.categoryId,
          aside: `${item.articleCount}篇`,
        };
      });
      return data;
    },
  },
  methods: {
    async fetchData() {
      return await getBlogTypes();
    },
    handleSelect(item) {
      if (item.id === -1) {
        this.$router.push({
          name: "Blog",
          query: {
            page: 1,
            limit: this.limit,
          },
        });
      } else {
        this.$router.push({
          name: "CategoryBlog",
          query: {
            page: 1,
            limit: this.limit,
          },
          params: {
            categoryId: item.id,
          },
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.blog-category-container {
  width: 300px;
  box-sizing: border-box;
  padding: 20px;
  position: relative;
  height: 100%;
  overflow-y: auto;
  h2 {
    font-weight: bold;
    letter-spacing: 2px;
    font-size: 1em;
    margin: 0;
  }
}
</style>